<?php

namespace App\Http\Controllers\Api;

use App\Model\CityModel;
use App\Model\CustomerModel;
use App\Model\UserAddressModel;
use Illuminate\Http\Request;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;


class CustomerController extends Controller
{
	//客户管理
	public function index(){
		$szm = array('A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','W','X','Y','Z','#');
		$data['szm'] = $szm;//首字母

		$data['cus'] = CustomerModel::letter(request()->uid,request()->keywords);
		$data['cus_cy'] = CustomerModel::getList(request()->uid,request()->keywords);
		return ['code'=>1,'data'=>$data];
	}



	//添加客户
	public function insert(){
		$auth_phone = '/^1\d{10}$/';
		// $auth_name = "/^[\x{4e00}-\x{9fa5}]{2,6}$/u";
		if(!request()->name){
			return ['code'=>0,'msg'=>retStrInfo(60)];
		}
		$str_user ='/[\'.,:;*?~`!@#$%^&+=)(<>{}]|5261\]|\[|\/|\\\|\"|\|/';
		if(preg_match($str_user,request()->name)){
			return ['code'=>0,'msg'=>'客户姓名不可包含特殊字符'];
		}
		$user_len = mb_strlen(request()->name,'UTF8');
	
		if($user_len > 6){
			return ['code'=>0,'msg'=>"请输入6位以内的姓名"];
		}
		if(!request()->phone){
			return ['code'=>0,'msg'=>retStrInfo(61)];
		}
		if (!preg_match($auth_phone, request()->phone)) {
			return ['code' => 0, 'msg' => '手机号输入有误'];
		}
		
		// $is_exit = CustomerModel::where('phone',request()->phone)->first();
		// if($is_exit){
		// 	return ['code'=>0,'msg'=>retStrInfo(70)];
		// }

		DB::beginTransaction();
		try {
			$auth_name = "/^[\x{4e00}-\x{9fa5}]{2,6}$/u";
			if (preg_match($auth_name, request()->name)) {
				$pin = app('pinyin');
				$pinyin =$pin->abbr(request()->name);
			}else{
				$pinyin = request()->name;
			}
			//首字母
			$head = pinying($pinyin);

			//添加客户
			$customer = CustomerModel::create([
				'uid' => request()->uid,
				'name' => request()->name,
				'phone' => request()->phone,
				'head' => $head,
			]);

			
			DB::commit();

			return ['code'=>1,'msg'=>retStrInfo(68)];
		}catch (Expectation $e) {
			DB::rollBack();
			return ['code' => 0, 'msg' => retStrInfo(69)];
		}
	}

	//删除客户
	public function delete(){
		//查询客户信息
		$customer = CustomerModel::where('id',request()->id)->first();
		if(!$customer){
			return ['code'=>0,'msg'=>retStrInfo(71)];
		}
		//删除客户信息,删除客户地址
		DB::beginTransaction();
		try {
			UserAddressModel::where('cid',request()->id)->delete();
			CustomerModel::where('id',request()->id)->delete();
			DB::commit();
			return ['code'=>1,'msg'=>retStrInfo(72)];
		}catch (Expectation $e) {
			DB::rollback();
			return ['code' => 0, 'msg' => retStrInfo(73)];
		}
	}

	//编辑客户
	public function update(){
		//手机号正则
		$auth_phone = '/^1\d{10}$/';
		//查询客户信息
		$customer = CustomerModel::where('id',request()->id)->first();
		if(!$customer){
			return ['code'=>0,'msg'=>retStrInfo(71)];
		}
		$str_user ='/[\'.,:;*?~`!@#$%^&+=)(<>{}]|5261\]|\[|\/|\\\|\"|\|/';
		if(preg_match($str_user,request()->name)){
			return ['code'=>0,'msg'=>'客户姓名不可包含特殊字符'];
		}
		if(!request()->name){
			return ['code'=>0,'msg'=>retStrInfo(60)];
		}
		$user_len = mb_strlen(request()->name,'UTF8');
	
		if($user_len > 6){
			return ['code'=>0,'msg'=>"请输入6位以内的姓名"];
		}
		if(!request()->phone){
			return ['code'=>0,'msg'=>retStrInfo(61)];
		}
		if (!preg_match($auth_phone, request()->phone)) {
			return ['code' => 0, 'msg' => '手机号输入有误'];
		}
		
		$is_exit = CustomerModel::where('phone',request()->phone)->where('id','<>',request()->id)->first();
		if($is_exit){
			return ['code'=>0,'msg'=>retStrInfo(70)];
		}

		DB::beginTransaction();
		try {
			$auth_name = "/^[\x{4e00}-\x{9fa5}]{2,6}$/u";
			if (preg_match($auth_name, request()->name)) {
				$pin = app('pinyin');
				$pinyin =$pin->abbr(request()->name);
			}else{
				$pinyin = request()->name;
			}
			//首字母
			$head = pinying($pinyin);
			CustomerModel::where('id',request()->id)->update([
				'name' => request()->name,
				'phone' => request()->phone,
				'head' => $head,
			]);
			DB::commit();
			return ['code'=>1,'msg'=>retStrInfo(74)];
			//添加客户
		}catch(Expectation $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>retStrInfo(75)];
		}
	}

	//编辑(信息)
	public function detail(){
		//客户信息
		$list = CustomerModel::where('id',request()->id)->first();
		if(!$list){
			return ['code'=>0,'msg'=>'客户信息有误'];
		}
		$data['cus']['id'] = $list['id'];
		$data['cus']['name'] = $list['name'];
		$data['cus']['phone'] = $list['phone'];
		
		//客户地址信息
		$address = UserAddressModel::where('cid',request()->id)->orderBy('is_default','asc')->orderBy('id','desc')->get();

		$site = [];
		if(count($address) > 0){
			foreach($address as $k=>$v){
				$province = CityModel::city($v['provinceid']);
				$city = CityModel::city($v['cityid']);
				$county = CityModel::city($v['countyid']);
				
				$site[$k]['id'] = $v['id'];
				$site[$k]['name'] = $v['name'];
				$site[$k]['phone'] = $v['phone'];
				$site[$k]['detail'] = $province.$city.$county.$v['detail'];
				$site[$k]['is_default'] = $v['is_default'];
			}
		}
		
		$data['address'] = $site;

		return ['code'=>1,'data'=>$data];
	}

	//添加地址
	public function add_site(Request $request){
		if(!request()->cid){
			return ['code'=>0,'msg'=>"请选择客户"];
		}
		if(!request()->linkman){
			return ['code'=>0,'msg'=>retStrInfo(62)];
		}
		if(!request()->linktel){
			return ['code'=>0,'msg'=>retStrInfo(63)];
		}
		$auth_phone = '/^1\d{10}$/';
		if (!preg_match($auth_phone, request()->linktel)) {
			return ['code' => 0, 'msg' => '手机号输入有误'];
		}
		if(!request()->provinceid){
			return ['code'=>0,'msg'=>retStrInfo(64)];
		}
		if(!request()->cityid){
			return ['code'=>0,'msg'=>retStrInfo(65)];
		}
		if(!request()->countyid){
			return ['code'=>0,'msg'=>retStrInfo(66)];
		}
		if(!request()->detail){
			return ['code'=>0,'msg'=>retStrInfo(67)];
		}
		$is_default = $request->input('is_default',2);
		if($is_default == 1){
			UserAddressModel::where('uid',request()->uid)->where('cid',request()->cid)->update(['is_default'=>2]);
		}
		DB::beginTransaction();
		try {
			//添加地址
			$address = UserAddressModel::create([
				'uid' => request()->uid,
				'cid' => request()->cid,
				'name' => request()->linkman,
				'phone' => request()->linktel,
				'provinceid' => request()->provinceid,
				'cityid' => request()->cityid,
				'countyid' => request()->countyid,
				'detail' => request()->detail,
				'is_default' => $is_default,
			]);
			DB::commit();
			return ['code'=>1,'msg'=>'添加成功'];
			//添加客户
		}catch(Expectation $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>'添加失败'];
		}
	}

	//编辑地址
	public function edit_site(){
		$aid = request()->aid;
		$address = UserAddressModel::where('id',$aid)->first();
		if(!$address){
			return ['code'=>"0",'msg'=>'该地址信息有误'];
		}
		$site['id'] = $address['id'];
		$site['name'] = $address['name'];
		$site['phone'] = $address['phone'];
		$site['provinceid'] = $address['provinceid'];
		$site['cityid'] = $address['cityid'];
		$site['countyid'] = $address['countyid'];
		$site['detail'] = $address['detail'];
		$site['is_default'] = $address['is_default'];
		$site['province'] = UserAddressModel::city($address['provinceid']);
		$site['city'] = UserAddressModel::city($address['cityid']);
		$site['county'] = UserAddressModel::city($address['countyid']);

		return ['code'=>1,'msg'=>'返回数据','data'=>$site];
	}

	//编辑客户地址
	public function update_site(Request $request){
		$aid = request()->aid;
		$address = UserAddressModel::where('id',$aid)->first();
		if(!$address){
			return ['code'=>0,'msg'=>"该地址信息有误"];
		}
		$post = $request->post();
        $validator = Validator::make($post,[
          'name'=>'required',
		  'phone'=>'required',
		  'provinceid'=>'required',
		  'cityid'=>'required',
		  'countyid'=>'required',
		  'detail'=>'required',
        ],[
          'name.required' => '收货人不能为空',
          'phone.required' => '联系方式不能为空',
		  'provinceid.required' => '请选择省市区',
		  'cityid.required' => '请选择省市区',
		  'countyid.required' => '请选择省市区',
		  'detail.required' => '请输入详细地址',
        ]);
        if($validator->fails()){
          return returnJson(0, $validator->errors()->first());
		}
		$auth_phone = '/^1\d{10}$/';
		if(!preg_match($auth_phone,request()->phone)){
			return ['code'=>0,'msg'=>"手机号输入有误"];
		}
		$is_default = $request->input('is_default',2);
		if($is_default == 1){
			UserAddressModel::where('cid',$address['cid'])->where('uid',request()->uid)->update(['is_default'=>2]);
		}
		DB::beginTransaction();
		try{
			UserAddressModel::where('id',$aid)->update([
				'name' => request()->name,
				'phone' => request()->phone,
				'provinceid' => request()->provinceid,
				'cityid' => request()->cityid,
				'countyid' => request()->countyid,
				'detail' => request()->detail,
				'is_default' => $is_default,
			]);
			DB::commit();
		}catch(Exception $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>"编辑失败"];
		}
		return ['code'=>1,'msg'=>"编辑成功"];

	}

	//删除地址
	public function delete_site(){
		$aid = request()->aid;
		$address = UserAddressModel::where('id',$aid)->first();
		if(!$address){
			return ['code'=>0,'msg'=>"该地址信息有误"];
		}
		DB::beginTransaction();
		try{
			UserAddressModel::where('id',$aid)->where('uid',request()->uid)->delete();
			DB::commit();
		}catch(Exception $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>"删除失败"];
		}
		return ['code'=>1,'msg'=>"删除成功"];

	}

	//设置默认地址
	public function is_default(){
		$aid = request()->aid;
		$address = UserAddressModel::where('id',$aid)->first();
		if(!$address){
			return ['code'=>0,'msg'=>"该地址信息有误"];
		}
		DB::beginTransaction();
		try{
			UserAddressModel::where('cid',$address['cid'])->where('uid',request()->uid)->update(['is_default'=>2]);
			UserAddressModel::where('id',$aid)->where('uid',request()->uid)->update(['is_default'=>1]);
			DB::commit();
		}catch(Exception $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>"设置失败"];
		}
		return ['code'=>1,'msg'=>"设置成功"];
	}

	

}